﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Sharper.Utils
{
    public class PathUtils
    {
        /// <summary>
        /// 是否为相对路径
        /// </summary>
        public static bool IsRelativePath(string sPath)
        {
            if (String.IsNullOrWhiteSpace(sPath))
                return true;

            sPath = sPath.Trim();
            string sTmp = Path.GetFullPath(sPath);
            if (sTmp.Length == sPath.Length)
                return false;

            return true;
        }

        /// <summary>
        /// 获取绝对路径
        /// </summary>
        public static string GetFullPath(string sPath, string sBasePath = null)
        {
            if (String.IsNullOrWhiteSpace(sPath))
                return sPath;

            if (!IsRelativePath(sPath))
                return sPath;

            if (sBasePath == null)
                return Path.GetFullPath(sPath);

            return sBasePath + '\\' + sPath;
        }

        /// <summary>
        /// 替换路径中禁止的符号
        /// </summary>
        /// <param name="sPath">路径</param>
        /// <param name="sReplaceChar">替换后的字符串</param>
        /// <returns></returns>
        public static string ReplaceLimitChar(string sPath, string sReplaceChar)
        {
            if (String.IsNullOrWhiteSpace(sPath))
                return sPath;
            sPath = sPath.Replace(":", sReplaceChar);
            sPath = sPath.Replace("/", sReplaceChar);
            sPath = sPath.Replace("?", sReplaceChar);
            sPath = sPath.Replace("<", sReplaceChar);
            sPath = sPath.Replace(">", sReplaceChar);
            sPath = sPath.Replace("|", sReplaceChar);
            sPath = sPath.Replace("\\", sReplaceChar);
            sPath = sPath.Replace("*", sReplaceChar);
            sPath = sPath.Replace("\"", sReplaceChar);

            if (String.IsNullOrWhiteSpace(sPath))
                return sPath;
            
            List<string> array = new List<string>();
            for (int i = 0; i < sPath.Length; i++)
                array.Add(sPath[i].ToString());
            
            int len = sPath.Length;
            while (len > 0)
            {
                if(array[len - 1] != ".")
                    break;
                array[len - 1] = sReplaceChar;
                len--;
            }
            sPath = string.Join("", array.ToArray());
            return sPath;
        }

        /// <summary>
        /// 获取目录下的文件名
        /// </summary>
        /// <param name="sPath"></param>
        /// <returns></returns>
        public static string[] GetFileNames(string sPath)
        {
            DirectoryInfo aDirectory = new DirectoryInfo(sPath);
            FileInfo[] Infos = aDirectory.GetFiles();
            if (Infos.Count() == 0)
                return null;

            string[] aRet = new string[Infos.Count()];
            for (int i = 0; i < aRet.Count(); i++)
                aRet[i] = Infos[i].FullName;
            
            return aRet;
        }
    }
}
